<?php
namespace Modules\Admin\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use Modules\Admin\Http\Requests\CommonStatusRequest;
use Modules\Admin\Http\Requests\CommonSortRequest;
use Modules\Admin\Http\Requests\CommonDeleteRequest;
use Modules\Admin\Models\Area as AreaModel;
use Modules\Admin\Http\Controllers\CommonController;
class AreaController extends CommonController
{
	/**
	 * @name  列表
	 */
    public function index(Request $request)
    { 
		$status = AreaModel::common_status();  
		$pid = $request->input('pid');
		$level_type = $request->input('level_type');
		if(!$pid){
			$pid = 0;
		}
		$s_pid = AreaModel::where(['id'=>$pid])->value('pid');
		if(!$level_type){
			$level_type = 1;
		}
        return view('admin::area.index',compact('pid','level_type','s_pid','status')); 
    }
	/**
	 * @name  列表数据
	 */
	 
    public function data(Request $request)
    {	
		$model = AreaModel::query();
		$params = $request->all();
		$model = query_condition($model,$params,'name');
        $model = $model->where('pid',$params['pid']);
        $model = $model->where('level_type',$params['level_type']);
        $res = $model
				->orderBy('id','asc')->orderBy('sort','asc')
				->paginate($params['limit'])
				->toArray(); 
        return response()->json(['code' => 0, 'msg'   => config('admin.limit_msg'),'count' => $res['total'],'data'  => $res['data'],'rel'=>1]);	
    }
	/**
	 * @name  添加页面
	 */
    public function create(Request $request)
    { 
		$pid = $request->input('pid');
		$level_type = $request->input('level_type');
		$name = AreaModel::where(['id'=>$pid])->value('name');
		$info = [
			'pid'=>$pid,
			'name'=>$name?$name:'省级',
		]; 
		return view('admin::area.create',compact('info','level_type'));
    }
	/**
	 * @name  添加数据处理 
	 */
    public function store(Request $request)
    {
		$model = new AreaModel();
		$data = $request->all();
		$data = $model->merger_name(array_filter($data));
		return $model->common_create($data,route('admin.area.index',['pid'=>$data['pid'],'level_type'=>$data['level_type']]));  
    }
	/**
	 * @name  编辑页面
	 */
    public function edit($id)
    {
		$info = AreaModel::find($id);
		return view('admin::area.edit',compact('info'));
    }
	/**
	 * @name  编辑数据处理
	 */
    public function update(Request $request,$id)
    {
		$model = new AreaModel();
		$data = $request->all();
		$data = $model->merger_name(array_filter($data));
		return $model->common_update($data,$id,route('admin.area.index',['pid'=>$data['pid'],'level_type'=>$data['level_type']])); 
    }
	/**
	 * @name  调整状态数据处理
	 */
    public function status(CommonStatusRequest $request,$id)
    {
		return (new AreaModel())->common_status_update($request->all(),$id,route('admin.area.index')); 
    }
	/**
	 * @name  排序数据处理
	 */
    public function sorts(CommonSortRequest $request,$id)
    {
		return (new AreaModel())->common_status_update($request->all(),$id,route('admin.area.index'));  
    }
	/**
	 * @name  删除数据处理
	 */
    public function destroy(CommonDeleteRequest $request)
    {
		return (new AreaModel())->area_delete($request->get('id'),route('admin.area.index'));  
    }
	/**
	 * @name  导入服务器数据
	 */
    public function import_data()
    {
		 Excel::load(public_path('upload/area').'/省市区数据.xls',function($reader){
			 $results = $reader->toArray(); //获取到数据
			 DB::beginTransaction();
			 try{
				 foreach($results as $k=>$v){
					 if(!$v['city_code']){
						 unset($v['city_code']);
					 }
					 if(!$v['zip_code']){
						 unset($v['zip_code']);
					 }
					 $v['created_at'] = date('Y-m-d H:i:s');
					 AreaModel::insert($v);
				 }
			    DB::commit();
			}catch(\Exception $e){
				DB::rollBack();	 
				return response()->json([
						'status'=>0,
						'error' => '导入失败！'
					],201);
			}	
		}); 
		return response()->json([
				'status'=>1,
				'msg' => '导入成功！'
			],200);	
    }
	/**
	 * @name  导出数据
	 */
    public function export_data()
    {
		$data[] =['id','父级','名称','简称','级别','区号','邮编','全称路径','经度','纬度','拼音','状态','排序']; 
		$area = AreaModel::select('id','pid','name','short_name','level_type','city_code','zip_code','merger_name','lng','lat','pinyin','status','sort')->get()->toArray();
		$data = array_merge($data,$area);
		Excel::create(iconv('UTF-8', 'GBK','省市区数据'),function($excel) use ($data){
			$excel->sheet('score',function($sheet) use ($data){
				$sheet->rows($data);
			});
		})->export('xls');
    }
	
	
	
	
	
	
}
